﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DoAn.DTO;
using System.Data;
using System.Windows.Forms;

namespace DoAn.DAO
{
    public class SanDauGiaDAO
    {
        public static void taoSanDauGia(SanDauGiaDTO san)
        {
            string sql="insert into SANDAUGIA(TenSan,HinhAnh,MoTa,MoTaChiTiet,GiaKhoiDiem,MuaNgay,GiaMuaNgay,NgayDangSP,NgayKetThucDang,HoTroTraSP,ThoiGianTraSP,HinhThucHoanLaiTien,DiaChiShip,TenTaiKhoan,GiaSanHienTai,SoNguoiRaGia,SoLanRaGia,NgayKetThucSan,HinhThucKetThuc,MaDanhMuc,KetThucSan) values(N'"
            +san.TenSan+"','"+san.HinhAnh+"',N'"+san.MoTa+"',N'"+san.MoTaChiTiet+"',"+san.GiaKhoiDiem.ToString()+",'"+san.MuaNgay.ToString()+"',"+san.GiaMuaNgay.ToString()
                                                                                                            +",'"+san.NgayDangSP.ToShortDateString()+"','"+san.NgayKetThucDang.ToShortDateString()+"','"+san.HoTroTraSP.ToString()+"',"+san.ThoiGianTraSP.ToString()
                                                                                                            +",N'"+san.HinhThucHoanLaiTien+"',N'"+san.DiaChiShip+"','"+san.TenTaiKhoan+"',"+san.GiaSanHienTai.ToString()+","+san.SoNguoiRaGia.ToString()+","+san.SoLanRaGia.ToString()+",'"+san.NgayKetThucSan.ToShortDateString()
                                                                                                                                                                                                                    +"','"+san.HinhThucKetThuc+"',"+san.MaDanhMuc.ToString()+",'"+san.KetThucSan.ToString()+"')";
            DataProvider.ExcuteQuery(sql);
        }

        public static int maSanDauGia()
        {
            string sql = "select max(MaSan) as MaSan from SANDAUGIA";
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);
            return int.Parse(dt.Rows[0]["MaSan"].ToString());
        }
        public static DataTable loadtopnew()
        {
            string sql = "select * from SANDAUGIA where Day(NgayDangSP)>'" + (DateTime.Now.Day-7) + "' and Month(NgayDangSp)='" + DateTime.Now.Month + "' and Year(NgayDangSP)='" + DateTime.Now.Year + "' and KetThucSan=1 ORDER BY GiaSanHienTai DESC ";
            DataTable dt = DataProvider.DataTable(sql);
            foreach (DataRow row in dt.Rows)
            {
                string dong = row["HinhAnh"].ToString();
                string[] dongarr = dong.Split(new char[] { ';' });
                row["HinhAnh"] = dongarr[0];
                //MessageBox.Show(dongarr[0].ToString());
            }
            return dt;
        }
        public static DataTable loadtopsan()
        {
            string sql = "select * from SANDAUGIA where KetThucSan=0 ORDER BY NgayDangSP DESC";
            DataTable dt = DataProvider.DataTable(sql);
            foreach (DataRow row in dt.Rows)
            {
                string dong = row["HinhAnh"].ToString();
                string[] dongarr = dong.Split(new char[] { ';' });
                row["HinhAnh"] = dongarr[0];
                //MessageBox.Show(dongarr[0].ToString());
            }
            return dt;
        }
        public static DataTable laydstheoDMcha(List<int> t)
        {
            string sql = "select * from SANDAUGIA where MaDanhMuc=";
            for (int i = 0; i < t.Count;i++ )
            {
                if (i < 1)
                    sql += t[i];
                else
                {
                    sql += " or MaDanhMuc=" + t[i];
                }
            }
            DataTable dt = DataProvider.DataTable(sql);
            dt.Columns.Add("muanhanh", typeof(string));
            foreach (DataRow row in dt.Rows)
            {
                string dong = row["HinhAnh"].ToString();
                string[] dongarr = dong.Split(new char[] { ';' });
                row["HinhAnh"] ="~/BAN/HinhAnh/"+ dongarr[0];
                bool a = Convert.ToBoolean(row["MuaNgay"]);
                if (a == true)
                    row["muanhanh"] = "Cho phép Mua nhanh";
                else
                    row["muanhanh"] = "Không Cho phép Mua nhanh";
                //MessageBox.Show(dongarr[0].ToString());
            }
            return dt;
        }
        //day day
        public static DataTable timkiemkhung(string sql)
        {
            DataTable dt = DataProvider.DataTable(sql);
            dt.Columns.Add("muanhanh", typeof(string));
            foreach (DataRow row in dt.Rows)
            {
                string dong = row["HinhAnh"].ToString();
                string[] dongarr = dong.Split(new char[] { ';' });
                row["HinhAnh"] = "~/BAN/HinhAnh/" + dongarr[0];
                bool a = Convert.ToBoolean(row["MuaNgay"]);
                if (a == true)
                    row["muanhanh"] = "Cho phép Mua nhanh";
                else
                    row["muanhanh"] = "Không Cho phép Mua nhanh";
                //MessageBox.Show(dongarr[0].ToString());
            }
            return dt;
        }
        public static DataTable laydstheoDMcon(int ma)
        {
            string sql = "select * from SanDauGia Where MaDanhMuc='" + ma.ToString() + "'";
            DataTable dt = DataProvider.DataTable(sql);
            dt.Columns.Add("muanhanh", typeof(string));
            foreach (DataRow row in dt.Rows)
            {
                string dong = row["HinhAnh"].ToString();
                string[] dongarr = dong.Split(new char[] { ';' });
                row["HinhAnh"] = "~/BAN/HinhAnh/" + dongarr[0];
                bool a =Convert.ToBoolean(row["MuaNgay"]);
                if (a == true)
                    row["muanhanh"] = "Cho phép Mua nhanh";
                else
                    row["muanhanh"] = "Không Cho phép Mua nhanh";
                //MessageBox.Show(dongarr[0].ToString());
            }
            return dt;
        }
        public static DataTable laydstheoten(string ten)
        {
            string sql = "select * from SanDauGia where TenSan LIKE N'"+"%"+ten+"%"+"'";
            DataTable dt = DataProvider.DataTable(sql);
            dt.Columns.Add("muanhanh", typeof(string));
            foreach (DataRow row in dt.Rows)
            {
                string dong = row["HinhAnh"].ToString();
                string[] dongarr = dong.Split(new char[] { ';' });
                row["HinhAnh"] = "~/BAN/HinhAnh/" + dongarr[0];
                bool a = Convert.ToBoolean(row["MuaNgay"]);
                if (a == true)
                    row["muanhanh"] = "Cho phép Mua nhanh";
                else
                    row["muanhanh"] = "Không Cho phép Mua nhanh";
                //MessageBox.Show(dongarr[0].ToString());
            }
            return dt;
        }
        public static DataTable laysptheoma(int ma)
        {
            string sql = "select * from SanDauGia where MaSan='"+ma.ToString()+"'";
            DataTable dt = DataProvider.DataTable(sql);
            dt.Columns.Add("muanhanh", typeof(string));
            foreach (DataRow row in dt.Rows)
            {
                //string dong = row["HinhAnh"].ToString();
                //string[] dongarr = dong.Split(new char[] { ';' });
                //row["HinhAnh"] = "~/BAN/HinhAnh/" + dongarr[0];
                bool a = Convert.ToBoolean(row["MuaNgay"]);
                if (a == true)
                    row["muanhanh"] = "Cho phép Mua nhanh";
                else
                    row["muanhanh"] = "Không Cho phép Mua nhanh";
                //MessageBox.Show(dongarr[0].ToString());
            }
            return dt;
        }
        public static void update_giahientai(int ma, float tien)
        {
            string sql = "update SANDAUGIA set GiaSanHienTai='" + tien.ToString() + "' where MaSan='" + ma.ToString() + "'";
            DataProvider.ExcuteQuery(sql);
        }
        public static void update_muangay(int ma,float mua)
        {
            string sql = "update SANDAUGIA set GiaSanHienTai='"+mua.ToString()+"',KetThucSan=1 where MaSan='"+ma.ToString()+"'";
            DataProvider.ExcuteQuery(sql);
        }

    }
}